﻿using DryIoc;
using HandyControl.Data;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLineNew.Common;
using ZoneLineNew.Models.Entity.BaseInfo;
using ZoneLineNew.Models.StationConfig;
using ZoneLineNew.Services.DB;
using ZoneLineNew.Services.Message;
using ZoneLineNew.ViewModelBase;

namespace ZoneLineNew.ViewModels {

    public class DeviceSettingViewModel : NavigationViewModel {

        public DeviceSettingViewModel(
            IRegionManager regionManager,
            IDialogService dialog,
            IMessageService messageBox,
            IEventAggregator eventAggregator,
            IDbOperate<IFreeSqlMainContext> dbOperate)
            : base(regionManager, dialog, messageBox, eventAggregator, dbOperate) {

        }


        #region 前减工站配置
        private QOP006StationConfig _qOP006StationConfig;
       
        public QOP006StationConfig QOP006StationConfig {
            get { return _qOP006StationConfig; }
            set { _qOP006StationConfig = value; base.RaisePropertyChanged(); }
        }

        private QOP020StationConfig _qOP020StationConfig;

        public QOP020StationConfig QOP020StationConfig {
            get { return _qOP020StationConfig; }
            set { _qOP020StationConfig = value; base.RaisePropertyChanged(); }
        }

        private QOP010StationConfig _qOP010StationConfig;

        public QOP010StationConfig QOP010StationConfig {
            get { return _qOP010StationConfig; }
            set { _qOP010StationConfig = value; base.RaisePropertyChanged(); }
        }

        private QOP050LStationConfig _qOP050LStationConfig;

        public QOP050LStationConfig QOP050LStationConfig {
            get { return _qOP050LStationConfig; }
            set { _qOP050LStationConfig = value; base.RaisePropertyChanged(); }
        }

        private QOP050RStationConfig _qOP050RStationConfig;

        public QOP050RStationConfig QOP050RStationConfig {
            get { return _qOP050RStationConfig; }
            set { _qOP050RStationConfig = value; base.RaisePropertyChanged(); }
        }

        private COP610StationConfig _cOP610StationConfig;

        public COP610StationConfig COP610StationConfig
        {
            get { return _cOP610StationConfig; }
            set { _cOP610StationConfig = value; base.RaisePropertyChanged(); }
        }
        #endregion

        #region 后减工站配置
        private HOP010StationConfig _hOP010StationConfig;

        public HOP010StationConfig HOP010StationConfig {
            get { return _hOP010StationConfig; }
            set { _hOP010StationConfig = value; base.RaisePropertyChanged(); }
        }

        private HOP040StationConfig _hOP040StationConfig;

        public HOP040StationConfig HOP040StationConfig {
            get { return _hOP040StationConfig; }
            set { _hOP040StationConfig = value; base.RaisePropertyChanged(); }
        }
        #endregion

        public DelegateCommand SaveCommand => new DelegateCommand(() => {
           
            var dialogAsk = _message.Ask("确定是否保存全部？");
            if (dialogAsk == System.Windows.MessageBoxResult.OK) {
                if (Cache.IsQopStaion) {

                    var res = _mainDbOperate.GetFreeSql().InsertOrUpdate<QOP006StationConfig>().SetSource(QOP006StationConfig).ExecuteAffrows();
                    res = _mainDbOperate.GetFreeSql().InsertOrUpdate<QOP020StationConfig>().SetSource(QOP020StationConfig).ExecuteAffrows();
                    res = _mainDbOperate.GetFreeSql().InsertOrUpdate<QOP010StationConfig>().SetSource(QOP010StationConfig).ExecuteAffrows();
                    res = _mainDbOperate.GetFreeSql().InsertOrUpdate<QOP050LStationConfig>().SetSource(QOP050LStationConfig).ExecuteAffrows();
                    res = _mainDbOperate.GetFreeSql().InsertOrUpdate<QOP050RStationConfig>().SetSource(QOP050RStationConfig).ExecuteAffrows();
                    res = _mainDbOperate.GetFreeSql().InsertOrUpdate<COP610StationConfig>().SetSource(COP610StationConfig).ExecuteAffrows();
                    _message.GrowSuccess("保存成功");

                } else if (Cache.IsHopStaion) {

                    var res = _mainDbOperate.GetFreeSql().InsertOrUpdate<HOP010StationConfig>().SetSource(HOP010StationConfig).ExecuteAffrows();
                    res = _mainDbOperate.GetFreeSql().InsertOrUpdate<HOP040StationConfig>().SetSource(HOP040StationConfig).ExecuteAffrows();
                    _message.GrowSuccess("保存成功");

                }
            }
        });



        protected override void OnViewLoaded() {

            if (Cache.IsQopStaion) {
            
                QOP006StationConfig = _mainDbOperate.GetModel<QOP006StationConfig>(o => true);
                QOP020StationConfig = _mainDbOperate.GetModel<QOP020StationConfig>(o => true);
                QOP010StationConfig = _mainDbOperate.GetModel<QOP010StationConfig>(o => true);
                QOP050LStationConfig = _mainDbOperate.GetModel<QOP050LStationConfig>(o => true);
                QOP050RStationConfig = _mainDbOperate.GetModel<QOP050RStationConfig>(o => true);
                COP610StationConfig = _mainDbOperate.GetModel<COP610StationConfig>(o => true);
            
            } else if (Cache.IsHopStaion) {

                HOP010StationConfig = _mainDbOperate.GetModel<HOP010StationConfig>(o => true);
                HOP040StationConfig = _mainDbOperate.GetModel<HOP040StationConfig>(o => true);
            }

            //base.OnViewLoaded();
        }





    }

}
